<template>
  <div class="containerHall">
    <el-dialog
      :title="dialogConfig.name"
      :visible.sync="dialogVisible"
      @close="dialogVisible = false"
      append-to-body
    >
      <el-form ref="formRef" :model="formData" :rules="rules">
        <el-form-item
          label="用户标签"
          :label-width="'100px'"
          prop="userTypeTagNoList"
        >
          <el-select
            v-model="formData.userTypeTagNoList"
            multiple
            placeholder="请选择"
            value-key="tagNo"
            @focus="handleFocus(3)"
          >
            <el-option
              v-for="item in optionsUser"
              :key="item.tagNo"
              :label="item.content"
              :value="item.tagNo"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="一级内容标签" :label-width="'100px'" prop="name">
          <el-select
            v-model="formData.contentTagNoList"
            multiple
            placeholder="请选择"
            value-key="tagNo"
            @focus="handleFocus(1)"
          >
            <el-option
              v-for="item in optionsContent"
              :key="item.tagNo"
              :label="item.content"
              :value="item.tagNo"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="话题名称" :label-width="'100px'" prop="link">
          <el-input v-model="formData.theme" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="话题插图" prop="picture" label-width="100px">
          <ul class="el-upload-list" style="display: flex; flex-wrap: wrap">
            <li v-if="formData.image != ''">
              <el-image
                :src="formData.image | imageUrl"
                style="width: 100px; height: 100px; margin: 20px"
                :fit="'contain'"
                @click="handleImageClicked(pic)"
              >
              </el-image>
              <i
                class="el-icon-close"
                style="position: absolute"
                @click="handleRemoveImage()"
              ></i>
            </li>
          </ul>
          <el-upload
            action="/console/api/upload/image"
            :show-file-list="false"
            :multiple="true"
            :on-success="handlePictureOK"
          >
            <el-button slot="trigger" size="small" type="primary"
              >选取文件</el-button
            >
          </el-upload>
        </el-form-item>

        <el-form-item label="话题内容" :label-width="'100px'" prop="seqencing">
          <el-input
            v-model="formData.description"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item label="排序" :label-width="'100px'" prop="seqencing">
          <el-input v-model="formData.sequence" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="审核" :label-width="'100px'" prop="seqencing">
          <el-select v-model="formData.forReview" placeholder="请选择">
            <el-option
              v-for="item in optionsForReview"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>

      <el-dialog :visible.sync="imageDialogVis" append-to-body>
        <el-image :fit="'contain'" :src="imageToView"></el-image>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import { topicUpdateApi, topicAddApi, tagListApi } from '@api/whisper'
export default {
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    value: {
      type: Object
    },
    request: {
      type: Object
    },
    dialogConfig: {
      type: Object
    }
  },
  data() {
    return {
      posting: false,
      formData: this.setup(this.value),
      rules: {},
      imageToView: null,
      imageDialogVis: false,
      optionsForReview: [
        {
          value: 0,
          label: '全部用户'
        },
        {
          value: 1,
          label: '审核专用'
        }
      ],
      optionsUser: [], // 用户标签
      optionsContent: [] // 内容标签
    }
  },
  created() {},
  mounted() {},
  methods: {
    setup() {
      if (!this.value) {
        return {
          description: '',
          forReview: 0,
          id: '',
          image: '',
          sequence: 0,
          theme: '',
          userTypeTagNoList: '',
          contentTagNoList: ''
        }
      } else {
        const clone = deepClone(this.value)
        console.log('clone---', clone)
        clone.contentTagNoList = clone.contentTagNoList.split(',')
        clone.userTypeTagNoList = clone.userTypeTagNoList.split(',')
        return clone
      }
    },

    handleConfirm() {
      this.posting = true
      let func = null
      if (this.formData.id) {
        func = topicUpdateApi
      } else {
        func = topicAddApi
      }
      const clone = deepClone(this.formData)
      clone.contentTagNoList = clone.contentTagNoList.join(',')
      clone.userTypeTagNoList = clone.userTypeTagNoList.join(',')
      func(clone)
        .then(res => {
          this.dialogVisible = false
          this.$emit('success') // 刷新
        })
        .catch(err => {
          this.$message('error')
        })
    },
    handleFocus(val) {
      tagListApi(val).then(res => {
        if (res) {
          if (val == 3) {
            // 用户标签
            this.optionsUser = res.data.list
          } else {
            // 内容标签
            this.optionsContent = res.data.list
            console.log('内容标签', this.optionsContent)
          }
        }
      })
    },

    // 上传图片
    handlePictureOK(pic) {
      this.formData.image = pic.data.url
    },
    handleRemoveImage(pictures, i) {
      this.formData.image = ''
    },
    handleImageClicked(pic) {
      this.imageToView = pic
      this.imageDialogVis = true
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.formData = this.setup()
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.containerHall {
}
</style>
